<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司ID" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行业领域" prop="industry">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入行业领域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工数量" prop="employees">
        <el-input
          v-model="queryParams.employees"
          placeholder="请输入员工数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度营收" prop="revenue">
        <el-input
          v-model="queryParams.revenue"
          placeholder="请输入年度营收"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标志" prop="delFlag">
        <el-select v-model="queryParams.delFlag" placeholder="请选择删除标志" clearable>
          <el-option
            v-for="dict in dict.type.bole_event_registration"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['internship:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['internship:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['internship:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['internship:company:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="公司ID" align="center" prop="companyId" /> -->
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="公司LOGO路径" align="center" prop="logoUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.logoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="行业领域" align="center" prop="industry" />
      <el-table-column label="员工数量" align="center" prop="employees" />
      <el-table-column label="年度营收" align="center" prop="revenue" />
      <el-table-column label="删除标志" align="center" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bole_event_registration" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['internship:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['internship:company:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司LOGO路径" prop="logoUrl">
          <image-upload v-model="form.logoUrl"/>
        </el-form-item>
        <el-form-item label="行业领域" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业领域" />
        </el-form-item>
        <el-form-item label="员工数量" prop="employees">
          <el-input v-model="form.employees" placeholder="请输入员工数量" />
        </el-form-item>
        <el-form-item label="年度营收(万)" prop="revenue">
          <el-input v-model="form.revenue" placeholder="请输入年度营收" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-select v-model="form.delFlag" placeholder="请选择删除标志">
            <el-option
              v-for="dict in dict.type.bole_event_registration"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">职位信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBolePosition">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBolePosition">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bolePositionList" :row-class-name="rowBolePositionIndex" @selection-change="handleBolePositionSelectionChange" ref="bolePosition">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="职位名称" prop="title" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.title" placeholder="请输入职位名称" />
            </template>
          </el-table-column>
          <el-table-column label="职位类型" prop="type" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.type" placeholder="请输入职位类型" />
            </template>
          </el-table-column>
          <el-table-column label="工作地点" prop="location" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.location" placeholder="请输入工作地点" />
            </template>
          </el-table-column>
          <el-table-column label="实习时长" prop="duration" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.duration" placeholder="请输入实习时长" />
            </template>
          </el-table-column>
          <el-table-column label="薪资待遇" prop="salary" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.salary" placeholder="请输入薪资待遇" />
            </template>
          </el-table-column>
          <el-table-column label="核心职责(句号分隔)" prop="duties" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.duties" placeholder="逗号分隔不同职责" />
            </template>
          </el-table-column>
          <el-table-column label="任职要求(句号分隔)" prop="requirements" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.requirements" placeholder="逗号分隔不同要求" />
            </template>
          </el-table-column>
          <el-table-column label="转正率(数字即可)" prop="conversionRate" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.conversionRate" placeholder="50%输入50" />
            </template>
          </el-table-column>
          <el-table-column label="晋升路径" prop="promotionPath" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.promotionPath" placeholder="请输入晋升路径" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="投递所需积分" prop="delFlag" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.delFlag" placeholder="请输入投递所需积分" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany } from "@/api/internship/company";

export default {
  name: "Company",
  dicts: ['bole_event_registration'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBolePosition: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司信息表格数据
      companyList: [],
      // 职位信息表格数据
      bolePositionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        companyName: null,
        logoUrl: null,
        industry: null,
        employees: null,
        revenue: null,
        createTime: null,
        delFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司信息列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: null,
        companyName: null,
        logoUrl: null,
        industry: null,
        employees: null,
        revenue: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.bolePositionList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyId = row.companyId || this.ids
      getCompany(companyId).then(response => {
        this.form = response.data;
        this.bolePositionList = response.data.bolePositionList;
        this.open = true;
        this.title = "修改公司信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bolePositionList = this.bolePositionList;
          if (this.form.companyId != null) {
            updateCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyIds = row.companyId || this.ids;
      this.$modal.confirm('是否确认删除公司信息编号为"' + companyIds + '"的数据项？').then(function() {
        return delCompany(companyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 职位信息序号 */
    rowBolePositionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 职位信息添加按钮操作 */
    handleAddBolePosition() {
      let obj = {};
      obj.title = "";
      obj.type = "";
      obj.location = "";
      obj.duration = "";
      obj.salary = "";
      obj.duties = "";
      obj.requirements = "";
      obj.conversionRate = "";
      obj.promotionPath = "";
      obj.createTime = "";
      obj.delFlag = "";
      this.bolePositionList.push(obj);
    },
    /** 职位信息删除按钮操作 */
    handleDeleteBolePosition() {
      if (this.checkedBolePosition.length == 0) {
        this.$modal.msgError("请先选择要删除的职位信息数据");
      } else {
        const bolePositionList = this.bolePositionList;
        const checkedBolePosition = this.checkedBolePosition;
        this.bolePositionList = bolePositionList.filter(function(item) {
          return checkedBolePosition.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBolePositionSelectionChange(selection) {
      this.checkedBolePosition = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('internship/company/export', {
        ...this.queryParams
      }, `company_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
